import { computed } from "vue";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
import store from "@/store";
import { isNumber } from 'lodash-es'
dayjs.extend(utc);
dayjs.extend(timezone);


const useTimezone = () => {
  // 获取当前系统的所在时区对应的偏移时差
const getCurrentTimezone = () => {
  const offsetInMinutes = dayjs().utcOffset();
  const offsetInHours = offsetInMinutes / 60;
  return offsetInHours;
};
// 时区对应的偏移时差
let sysTimezone = getCurrentTimezone();
const timezones = [
  { value: -12, label: "UTC-12" },
  { value: -11, label: "UTC-11" },
  { value: -10, label: "UTC-10" },
  { value: -9, label: "UTC-09" },
  { value: -8, label: "UTC-08" },
  { value: -7, label: "UTC-07" },
  { value: -6, label: "UTC-06" },
  { value: -5, label: "UTC-05" },
  { value: -4, label: "UTC-04" },
  { value: -3, label: "UTC-03" },
  { value: -2, label: "UTC-02" },
  { value: -1, label: "UTC-01" },
  { value: 0, label: "UTC+00" },
  { value: 1, label: "UTC+01" },
  { value: 2, label: "UTC+02" },
  { value: 3, label: "UTC+03" },
  { value: 4, label: "UTC+04" },
  { value: 5, label: "UTC+05" },
  { value: 6, label: "UTC+06" },
  { value: 7, label: "UTC+07" },
  { value: 8, label: "UTC+08" },
  { value: 9, label: "UTC+09" },
  { value: 10, label: "UTC+10" },
  { value: 11, label: "UTC+11" },
  { value: 12, label: "UTC+12" },
  { value: 13, label: "UTC+13" },
  { value: 14, label: "UTC+14" },
];
// 用户设置的当前时区
const  currentTimezone = computed(() => {
  return store.getters?.userInfo?.utc_offset || sysTimezone
});
// // 根据相应时区显示相应时区所在的时间   格式是 YYYY-MM-DD HH:mm:ss
const convertTimeToTimezone = (time, tz = currentTimezone.value,format='YYYY-MM-DD HH:mm:ss') => {

  const offsetInMinutes = (isNumber(tz))? tz * 60: sysTimezone * 60;
  // 将时间转换为指定时区时间
  const date = dayjs(time).utcOffset(offsetInMinutes);
  return date.format(format);
};
const getTimezoneList = () => {
  return timezones;
};
//a-date-picker  禁用日期函数
const disabledDate = (current) => {
    const offsetInMinutes = currentTimezone.value * 60;
    const nowInTargetTimezone = dayjs().utcOffset(offsetInMinutes).startOf("day");
    return current && current < nowInTargetTimezone;
};

//a-date-picker  禁用时间函数
const disabledDateTime = (choosTime) => {
    const offsetInMinutes = currentTimezone.value * 60;
    const nowInTargetTimezone = dayjs().utcOffset(offsetInMinutes);

    // 如果选择的时间是当前日期
    if (dayjs(choosTime).utcOffset(offsetInMinutes).isSame(nowInTargetTimezone, 'day')) {
        const currentHour = nowInTargetTimezone.hour();
        const currentMinute = nowInTargetTimezone.minute();

        const disabledHours = () => Array.from({ length: 24 }, (_, index) => index).splice(0, currentHour);

        const disabledMinutes = (selectedHour) => {
            if (selectedHour === currentHour) {
                return Array.from({ length: 60 }, (_, index) => index).splice(0, currentMinute + 1);
            }
            return [];
        };

        return {
            disabledHours,
            disabledMinutes,
        };
    }

    // 如果选择的时间不是当前日期，则不禁用任何时间
    return {
        disabledHours: () => [],
        disabledMinutes: () => [],
    };
  };
  return {
    getTimezoneList,
    convertTimeToTimezone,
    currentTimezone,
    disabledDate,
    disabledDateTime,
  };
};

export default useTimezone;
